/*
 *
 *      Copyright (c) 2018-2025, madp All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: 成都文思海辉金信软件有限公司
 *
 */

package com.pactera.madp.admin.controller;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.admin.api.constant.UpmsConstant;
import com.pactera.madp.admin.api.dto.*;
import com.pactera.madp.admin.api.entity.*;
import com.pactera.madp.admin.api.feign.RemoteWxService;
import com.pactera.madp.admin.api.vo.DeptLineDeptVO;
import com.pactera.madp.admin.api.vo.SysDeptVo;
import com.pactera.madp.admin.api.vo.UserVO;
import com.pactera.madp.admin.api.vo.WxEmpVo;
import com.pactera.madp.admin.service.*;
import com.pactera.madp.common.core.constant.SecurityConstants;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.common.security.annotation.Inner;
import com.pactera.madp.common.security.service.MadpUser;
import com.pactera.madp.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author madp
 * @since 2018-01-20
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/dept")
@Api(value = "dept", tags = "部门管理模块")
public class SysDeptController {
	private final SysDeptRelationService relationService;
	private final SysDeptService sysDeptService;
	private final SysWxDeptRelationService sysWxDeptRelationService;
	private final RemoteWxService remoteWxService;
	private final SysDeptUserService sysDeptUserService;
	private final SysUserService sysUserService;


	/**
	 * 通过ID查询
	 *
	 * @param id ID
	 * @return SysDept
	 */
	@GetMapping("/{id}")
	public R getById(@PathVariable Integer id) {
		return R.ok(sysDeptService.getById(id));
	}


	/**
	 * 返回树形菜单集合
	 *
	 * @return 树形菜单
	 */
//	@GetMapping(value = "/tree")
//	public R getTree() {
//		return R.ok(sysDeptService.selectTree());
//	}

	@GetMapping(value = "/tree")
	public R getTreeLike(
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "selectLine", required = false, defaultValue = "true") boolean selectLine
	) {
		List<DeptTree> list;
		if (name == null || name.isEmpty()) {
			list = sysDeptService.selectTree(selectLine);
		} else {
			list = sysDeptService.selectTreeLike(name);
		}
		list.sort(Comparator.comparing(DeptTree::getDeptType));
		return R.ok(list);
	}

	/*@GetMapping(value = "/tree")
	public R getTreeLike(@RequestParam(value = "name", required = false) String name) {
		List<DeptTree> list = sysDeptService.selectTreeLikeNew(name);
		list.sort(Comparator.comparing(DeptTree::getDeptType));
		return R.ok(list);
	}*/

	/**
	 * 添加
	 *
	 * @param sysDept 实体
	 * @return success/false
	 */
	@SysLog("添加部门")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_dept_add')")
	public R save(@Valid @RequestBody SysDept sysDept) {
		Integer sort = sysDept.getSort();
		if (sort == null )
			sysDept.setSort(0);
		if (sysDept != null) {
			Integer deptType = sysDept.getDeptType();
			if (deptType == UpmsConstant.DEPT_TYPE_ORGAN) {
				//设置企业微信新增部门对象
				SysWxDeptRelation sysWxDeptRelation = sysWxDeptRelationService.getOne(Wrappers.<SysWxDeptRelation>lambdaQuery()
						.eq(SysWxDeptRelation::getSysDeptId,sysDept.getParentId()));
				WxDeptDto dto = new WxDeptDto();
				dto.setName(sysDept.getName());
				dto.setParentid(sysWxDeptRelation.getWxDeptId());
				dto.setOrder(sysDept.getSort());
				//远程企业微信创建服务调用
				R<Integer> result = remoteWxService.createDept(dto, SecurityConstants.FROM_IN);

				//创建系统部门
				sysDeptService.saveDept(sysDept);

				//新增关联
				sysWxDeptRelation.setWxDeptId(result.getData());
				sysWxDeptRelation.setSysDeptId(sysDept.getDeptId());
				return R.ok(sysWxDeptRelationService.save(sysWxDeptRelation));
			} else if (deptType == UpmsConstant.DEPT_TYPE_LINE) {
				Integer deptId = SecurityUtils.getUser().getDeptId();
				if (deptId != 1)
					return R.failed("当前用户不是总行的人不允许创建条线部门");
				String name = sysDept.getName();
				Integer count = sysDeptService.count(
						Wrappers.<SysDept>lambdaQuery().likeLeft(SysDept::getName, name)
				);
				if (count > 0) {
					return R.failed(name + "条线已存在，请勿重复创建");
				}
				return R.ok(sysDeptService.saveLineDept(sysDept));
			}
		}
		return R.failed("传入部门对象为null");
	}

	@SysLog("条线成员调整")
	@PostMapping("/updateUsersToLineDept")
//	@PreAuthorize("@pms.hasPermission('sys_dept_add')")
	public R updateUsersToLineDept(@Validated @RequestBody LineDeptUserDTO dto) {
		Integer lineDeptId = dto.getLineDeptId();
		SysDept sysDept = sysDeptService.getById(lineDeptId);
		if (sysDept == null)
			return R.failed("部门不存在或已被删除");
		Integer deptType = sysDept.getDeptType();
		if (deptType != UpmsConstant.DEPT_TYPE_LINE)
			return R.failed("该部门类型 1-机构，不允许该操作!");
		return R.ok(sysDeptUserService.updateUsersToLineDept(dto));
	}

	@GetMapping("/getLineDeptChildrens/{lineDeptId}")
	public R getLineDeptChildrens(@PathVariable("lineDeptId") Integer lineDeptId) {
		Map<String, Object> returnResult = new HashMap<>();
		List<DeptLineDeptVO> deptChildrens = sysDeptService.getChildrens(lineDeptId);
		returnResult.put("depts", deptChildrens);
		List<Map<String, Object>> userList = new ArrayList<>();
		Integer deptId = sysDeptService.getDeptId(lineDeptId);
		sysUserService.list(
				Wrappers.<SysUser>lambdaQuery()
				.eq(SysUser::getDeptId, deptId)
				.orderByAsc(SysUser::getUsername)
		).stream().forEach(s -> {
			R<WxEmpVo> result = remoteWxService.getEmp(s.getPhone(), SecurityConstants.FROM_IN);
			if (result != null && result.getCode() == 0) {
				Map<String, Object> map = new HashMap<>();
				map.put("userId", s.getUserId());
				map.put("cname", result.getData().getName());
				userList.add(map);
			}
		});
		returnResult.put("users", userList);
		return R.ok(returnResult);
	}

	@GetMapping("/getLineDeptSelected/{deptId}")
	public R getLineDeptSelected(@PathVariable("deptId") Integer deptId) {
		SysDeptVo sysDeptVo = sysDeptUserService.getLineDeptSelected1(deptId);
		Map<String, Object> returnResult = new HashMap<>();
		if (sysDeptVo != null) {
			List<Integer> selectDeptIds = sysDeptVo.getSelectLineDeptIds();
			List<DeptLineDeptVO> deptList = new ArrayList<>();
			if (selectDeptIds != null && selectDeptIds.size() > 0) {
				deptList = sysDeptService.getSelectDepts(selectDeptIds);
			}
			returnResult.put("selectDepts", deptList);
			List<Integer> selectUserIds = sysDeptVo.getSelectUserIds();
			List<Map<String, Object>> userList = new ArrayList<>();
			if (selectUserIds != null && selectUserIds.size() > 0) {
				List<SysUser> selectUsers = sysUserService.list(
						Wrappers.<SysUser>lambdaQuery()
								.in(SysUser::getUserId, selectUserIds)
								.orderByAsc(SysUser::getUsername)
				);

				for (SysUser sysUser : selectUsers) {
					R<WxEmpVo> result = remoteWxService.getEmp(sysUser.getPhone(), SecurityConstants.FROM_IN);
					if (result != null && result.getCode() == 0) {
						Map<String, Object> map = new HashMap<>();
						map.put("userId", sysUser.getUserId());
						map.put("cname", result.getData().getName());
						userList.add(map);
					}
				}
			}
			returnResult.put("selectUsers", userList);
		} else {
			returnResult.put("selectDepts", new ArrayList<>());
			returnResult.put("selectUsers", new ArrayList<>());
		}
		return R.ok(returnResult);
	}

	@Inner(value = false)
	@GetMapping("/getDeptsAndUsers/{deptId}")
	public R getDeptsAndUsers(@PathVariable("deptId") Integer deptId) {
		SysDept sysDept = sysDeptService.getById(deptId);
		if (sysDept == null)
			return R.failed("部门不存在或已被删除");
		Map<String, Object> returnResult = new HashMap<>();
		Integer deptType = sysDept.getDeptType();
		if (deptType == UpmsConstant.DEPT_TYPE_LINE) {
			SysDeptVo sysDeptVo = sysDeptUserService.getLineDeptSelected1(deptId);
			if (sysDeptVo != null) {
//				List<Integer> selectLineDeptIds = sysDeptVo.getSelectLineDeptIds();
//				if (selectLineDeptIds != null && selectLineDeptIds.size() > 0) {
//					List <Integer> wxDeptIds = sysDeptService.getAllDeptsLine(selectLineDeptIds);
//
//				}
				List<Integer> selectUserIds = sysDeptVo.getSelectUserIds();
				if (selectUserIds != null && selectUserIds.size() > 0) {
					List<String> userMobiles = sysUserService.list(
							Wrappers.<SysUser>lambdaQuery()
							.select(SysUser::getPhone)
							.in(SysUser::getUserId, selectUserIds)
					).stream().map(s -> s.getPhone()).collect(Collectors.toList());
					returnResult.put("userMobiles", userMobiles);
				}
			}
		} else if (deptType == UpmsConstant.DEPT_TYPE_ORGAN) {
			List<Integer> wxDeptIds = sysDeptService.getAllDeptsOrg(deptId);
			returnResult.put("wxDeptIds", wxDeptIds);
		}
		return R.ok(returnResult);
	}

	@Inner(value = false)
	@GetMapping("/getDeptsAndUsers")
	@ApiOperation("getDeptsAndUsersByDeptIds")
	public R getDeptsAndUsers(@RequestParam(name = "deptIds") List<Integer> deptIds) {
		Collection<SysDept> sysDepts = sysDeptService.listByIds(deptIds);
		if (sysDepts == null) {
			return R.failed("部门列表为空");
		}
		Map<String, Object> returnResult = new HashMap<>();
		List<Integer> lineIds = sysDepts.stream()
				.filter(d -> d.getDeptType().equals(UpmsConstant.DEPT_TYPE_LINE))
				.map(SysDept::getDeptId)
				.collect(Collectors.toList());
		List<Integer> organIds = sysDepts.stream()
				.filter(d -> d.getDeptType().equals(UpmsConstant.DEPT_TYPE_ORGAN))
				.map(SysDept::getDeptId)
				.collect(Collectors.toList());
		log.info("lineIds:{}",lineIds);
		log.info("organIds:{}",organIds);
		if (!lineIds.isEmpty()) {
            SysDeptVo sysDeptVo = sysDeptUserService.getLineDeptSelected1(lineIds);
            if (sysDeptVo != null) {
                List<Integer> selectUserIds = sysDeptVo.getSelectUserIds();
                if (selectUserIds != null && selectUserIds.size() > 0) {
                    List<String> userMobiles = sysUserService.list(
                            Wrappers.<SysUser>lambdaQuery()
                                    .select(SysUser::getPhone)
                                    .in(SysUser::getUserId, selectUserIds)
                    ).stream().map(s -> s.getPhone()).distinct().collect(Collectors.toList());
                    returnResult.put("userMobiles", userMobiles);
                    log.info("userMobiles:{}",userMobiles);
                }
            }
        }

		if (!organIds.isEmpty()) {
            List<Integer> wxDeptIds = sysDeptService.getAllDeptsOrg(deptIds);
			wxDeptIds.removeIf(v->
					v == null
			);
            returnResult.put("wxDeptIds", wxDeptIds);
            log.info("wxDeptIds:{}",wxDeptIds);
		}
		return R.ok(returnResult);
	}

	@Inner(value = false)
	@GetMapping("/getChildrens/{deptId}")
	public R getChildrens(@PathVariable("deptId") Integer deptId) {
		SysDept sysDept = sysDeptService.getById(deptId);
		if (sysDept == null)
			return R.failed("部门不存在或已被删除");
		Integer deptType = sysDept.getDeptType();
		List<Map<String, Integer>> wxDeptIds = sysDeptService.getChildrensOrg(deptId, deptType);
		return R.ok(wxDeptIds);
	}

	@Inner(value = false)
	@GetMapping("/getAllChildrenOrgCode/{deptId}")
	public R getAllChildrenOrgCode(@PathVariable("deptId") Integer deptId) {
		SysDept sysDept = sysDeptService.getById(deptId);
		if (sysDept == null)
			return R.failed("部门不存在或已被删除");
		List<String> orgCodes = sysDeptService.getAllChildrenOrgCode(deptId);
		return R.ok(orgCodes);
	}

	@Inner(value = false)
	@PostMapping("/getChildrenPage/{deptId}")
	@ApiOperation(value = "按分页列表查询子级")
	public R getChildrenPage(@PathVariable("deptId") Integer deptId, @RequestBody Page page){
		SysDept sysDept = sysDeptService.getById(deptId);
		if (sysDept == null)
			return R.failed("部门不存在或已被删除");
		Integer deptType = sysDept.getDeptType();
		IPage depts = sysDeptService.getChildrenPage(deptId, deptType, page);
		return R.ok(depts);
	}

	@Inner(value = false)
	@PostMapping("/getChildrenPageByObjectId/{deptId}")
	@ApiOperation(value = "按分页列表精确查询子级")
	public R getChildrenPageByObjectId(@PathVariable("deptId") Integer deptId, @RequestBody Page page,Integer objectId){
		SysDept sysDept = sysDeptService.getById(deptId);
		if (sysDept == null)
			return R.failed("部门不存在或已被删除");
		Integer deptType = sysDept.getDeptType();
		IPage depts = sysDeptService.getChildrenPageByObjectId(deptId, deptType, page,objectId);
		return R.ok(depts);
	}

	@Inner(value = false)
	@GetMapping("/getWxDeptId/{deptId}")
	public R getWxDeptId(@PathVariable("deptId") Integer deptId) {
		SysDept sysDept = sysDeptService.getById(deptId);
		if (sysDept == null)
			return R.failed("部门不存在或已被删除");
		Integer wxDeptId = sysDeptService.getWxDeptId(sysDept);
		return R.ok(wxDeptId);
	}

	/**
	 * 删除
	 *
	 * @param id ID
	 * @return success/false
	 */
	@SysLog("删除部门")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_dept_del')")
	public R removeById(@PathVariable Integer id) {
        SysDept sysDept = sysDeptService.getById(id);
        if (sysDept == null)
            return R.failed("部门不存在或已被删除");
        Integer deptType = sysDept.getDeptType();
        if (deptType == UpmsConstant.DEPT_TYPE_LINE) {
            return R.ok(sysDeptService.deleteLineDept(id));
        } else if (deptType == UpmsConstant.DEPT_TYPE_ORGAN) {
            //设置企业微信删除部门对象
            SysWxDeptRelation sysWxDeptRelation = sysWxDeptRelationService.getOne(Wrappers.<SysWxDeptRelation>lambdaQuery()
                    .eq(SysWxDeptRelation::getSysDeptId,id));
            //远程企业微信创建服务调用
            remoteWxService.deleteDept(sysWxDeptRelation.getWxDeptId(), SecurityConstants.FROM_IN);

            //删除系统部门
            sysDeptService.removeDeptById(id);

            //删除系统与企微关联
            QueryWrapper<SysWxDeptRelation> wrapper = new QueryWrapper<>();
            wrapper.setEntity(sysWxDeptRelation);
            return R.ok(sysWxDeptRelationService.remove(wrapper));
        }
        return R.failed(Boolean.FALSE);
	}

	/**
	 * 编辑
	 *
	 * @param sysDept 实体
	 * @return success/false
	 */
	@SysLog("编辑部门")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_dept_edit')")
	public R update(@Valid @RequestBody SysDept sysDept) {
		if (sysDept == null)
			return R.failed("传入部门对象为null");
		Integer deptType = sysDept.getDeptType();
		if (deptType == UpmsConstant.DEPT_TYPE_ORGAN) {
			//设置企业微信更新部门对象
			SysWxDeptRelation sysWxDeptRelation = sysWxDeptRelationService.getOne(Wrappers.<SysWxDeptRelation>lambdaQuery()
					.eq(SysWxDeptRelation::getSysDeptId,sysDept.getDeptId()));
			WxDeptDto dto = new WxDeptDto();
			dto.setName(sysDept.getName());
			dto.setId(sysWxDeptRelation.getWxDeptId());
			//远程企业微信创建服务调用
			remoteWxService.updateDept(dto, SecurityConstants.FROM_IN);

			//更新系统部门
			sysDept.setUpdateTime(LocalDateTime.now());
			return R.ok(sysDeptService.updateDeptById(sysDept));
		} else {
			return sysDeptService.updateLineDept(sysDept);
		}

	}

	/**
	 * 查收子级列表
	 *
	 * @return 返回子级部门id列表
	 */
	@GetMapping(value = "/getDescendantList/{deptId}")
	public R<List<Integer>> getDescendantList(@PathVariable("deptId") Integer deptId) {
		return R.ok(relationService.list(Wrappers.<SysDeptRelation>lambdaQuery()
				.eq(SysDeptRelation::getAncestor, deptId))
				.stream()
				.map(SysDeptRelation::getDescendant)
				.collect(Collectors.toList()));
	}

	/**
	 * 查询系统与企业微信部门关联集合
	 * @return
	 */
	@Inner
	@GetMapping("/relations")
	public R<List<SysWxDeptRelation>> getSysWxRelations() {
		return R.ok(sysWxDeptRelationService.list());
	}

	/**
	 * 查询系统与企业微信部门关联集合
	 * @return
	 */
	@Inner
	@GetMapping("/relation/{id}")
	public R<SysDept> getSysWxRelationById(@PathVariable("id") Integer id) {
		SysWxDeptRelation relation = sysWxDeptRelationService.getOne(Wrappers.<SysWxDeptRelation>lambdaQuery()
				.eq(SysWxDeptRelation::getWxDeptId,id));
		if(relation == null) {
			return R.ok(new SysDept());
		}
		SysDept sysDept = sysDeptService.getById(relation.getSysDeptId());
		if(sysDept == null) {
			return R.ok(new SysDept());
		}
		return R.ok(sysDept);
	}

	/**
	 * 企业微信与系统部门同步
	 * @param
	 * @return
	 */
	@Inner
	@PostMapping("/sync")
	public void onSyncWxDept(@RequestBody Map<String,String> param) {
        if(param != null && !StrUtil.isEmpty((String)param.get("trees"))) {
			List<SyncSysDept> trees = JSONUtil.toBean(param.get("trees"), new TypeReference<List<SyncSysDept>>(){},false);
			sysDeptService.onSyncWxDept(trees);
		}
	}

	@Inner(value = false)
	@GetMapping("/selected-depts/{mobile}")
	public R getSelectedDepts(@PathVariable("mobile") String mobile) {
		Set<Integer> deptIds = sysDeptService.getSelectedDepts(mobile);
		return R.ok(deptIds);
	}

	@GetMapping("/lower-depts/{deptId}")
	@ApiOperation(value = "根据部门id获取下一级部门列表")
	public R<List<SysDept>> getLowerDepts(@PathVariable("deptId") Integer deptId) {
		List<SysDept> deptList = sysDeptService.list(new LambdaQueryWrapper<SysDept>().eq(SysDept::getParentId,deptId));
		return R.ok(deptList);
	}


	@GetMapping("/users/{deptId}")
	@ApiOperation(value = "根据部门id获取用户")
	public R<List<SysUser>> getUsers(@PathVariable("deptId") Integer deptId) {
		List<SysUser> userList = sysUserService.list(new LambdaQueryWrapper<SysUser>().eq(SysUser::getDeptId,deptId));
		return R.ok(userList);
	}

	@GetMapping("/getWxDeptIds")
	public R<List<Integer>> getWxDeptIds(@RequestParam(name = "deptIds") List<Integer> deptIds) {
		if (deptIds.isEmpty()) {
			return R.failed("deptIds为空");
		}
		List<Integer> wxDeptIds = sysWxDeptRelationService.list(new LambdaQueryWrapper<SysWxDeptRelation>()
				.in(SysWxDeptRelation::getSysDeptId, deptIds))
				.stream()
				.map(SysWxDeptRelation::getWxDeptId)
				.collect(Collectors.toList());
		return R.ok(wxDeptIds);
	}

	@Inner
	@PostMapping("/list")
	public R<List<SysDept>> getSysDeptList(@RequestBody List<Integer> ids) {
		return R.ok((List<SysDept>) sysDeptService.listByIds(ids));
	}

	@Inner(value = false)
	@GetMapping("/depts")
	public List<Map<String, Object>> depts(@RequestParam(name = "deptIds") List<Integer> deptIds) {
		if (deptIds == null || deptIds.size() == 0) {
			return new ArrayList<>();
		}
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.select("dept_id as deptId, name as deptName");
		wrapper.in("dept_id", deptIds);
		wrapper.eq("del_flag", 0);
		return sysDeptService.listMaps(wrapper);
	}

	@GetMapping("/treeRoot")
	public List<Map<String, Object>> treeRoot() {

		return null;
	}

	@Inner(value = false)
	@GetMapping("/remote/{wxDeptId}")
	Integer deptId(@PathVariable("wxDeptId") Integer wxDeptId) {
		SysWxDeptRelation one = sysWxDeptRelationService.getOne(
				Wrappers.<SysWxDeptRelation>lambdaQuery()
						.select(SysWxDeptRelation::getSysDeptId)
						.eq(SysWxDeptRelation::getWxDeptId, wxDeptId)
		);
		if (one != null)
			return one.getSysDeptId();
		return null;
	}

	@Inner(value = false)
	@GetMapping("/remote/getAllZhLine")
	List<Integer> getAllZhLine() {
		List<Integer> zhDeptId = sysDeptService.getAllZhLine();
		return zhDeptId;
	}


	@GetMapping("/username-dept")
	@ApiOperation("根据部门id获取成员账号消息")
	@SysLog("根据部门id获取成员账号消息")
	public List<String> getUserNameByDeptId(@RequestParam(value = "deptId") Integer deptId){
		List<String> list = new LinkedList<>();

		List<UserVO> userPage = new LinkedList<>();

		SysDept sysDept = sysDeptService.getById(deptId);
		Integer deptType = sysDept.getDeptType();
		if (!deptType.equals( UpmsConstant.DEPT_TYPE_ORGAN)  && !deptType.equals(UpmsConstant.DEPT_TYPE_LINE) ){
			return new LinkedList<>();
		}
		if (deptType.equals(UpmsConstant.DEPT_TYPE_ORGAN)) {
			userPage = sysUserService.getUserVosByDept(deptId);
		} else {
			userPage = sysUserService.getUsersWithLineDeptByList(deptId);
		}

		userPage.stream().forEach(l->{
			list.add(l.getUsername());
		});

		return list;
	}

	@GetMapping("/dept-by-id")
	@ApiOperation("根据部门id获取部门信息")
	@Inner(false)
	public List<SysDept> getDeptListById(@RequestParam("ids") List<Integer> ids){
		return sysDeptService.list(Wrappers.<SysDept>lambdaQuery().in(SysDept::getDeptId,ids));
	}

	@GetMapping("/dept-by")
	@ApiOperation("根据部门id获取部门信息")
	@Inner(false)
	public SysDept getDeptListBy(@RequestParam("id") Integer id){
		return sysDeptService.getById(id);
	}


//	@PostMapping("/lower-list")
//	@ApiOperation(value = "根据部门id列表获取下级部门列表")
//	@Inner(false)
//	public List<SysDept> getLowerDeptsByList(@RequestParam("deptIds") List<Integer> deptIds) {
//		return sysDeptService.getLowerDeptsByList(deptIds);
//	}

	@PostMapping("/lower-list")
	@ApiOperation(value = "根据部门id列表获取下级部门列表")
	@Inner(false)
	public List<SysDept> getLowerDeptsByList(@RequestBody List<Integer> deptIds) {
		return sysDeptService.getLowerDeptsByList(deptIds);
	}

}
